package com.xh.controller; 


import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.system.comm.util.DateUtils;
import com.system.comm.util.JsonUtils;
import com.system.common.pojo.BisCustomUser;
import com.system.common.pojo.BisShop;
import com.xh.service.BisCustomUserService;
import com.xh.service.BisShopService;

@Controller
@RequestMapping("custom")
public class BisCustomUserController {
	@Autowired
	private BisCustomUserService bisCustomUserService;
	@Autowired
	private BisShopService bisShopService;
     @RequestMapping("list")
     public String listAll(Model model,HttpServletRequest request){
    	 Map<String,Object> map=mohuQuery(request);
    	 List<Map<String,Object>> customList = bisCustomUserService.listAll(map);
    	 model.addAttribute("customList",customList);
    	 List<BisShop> shopList=bisShopService.queryBisShop();
    	 model.addAttribute("shopList",shopList);
    	 return "/bcujsp/bisCustomUserList";
     }
     private Map<String,Object> mohuQuery(HttpServletRequest request){
    	 Map<String,Object> map=new HashMap<>();
    	 String qmobile=request.getParameter("qmobile");
    	 String qusername=request.getParameter("qusername");
    	 String qshopid=request.getParameter("qshopid");
    	 String qenrollmode=request.getParameter("qenrollmode");
    	 String qcustomuserid=request.getParameter("qcustomuserid");
    	 String qnikename=request.getParameter("qnikename");
    	 String qwechatstatus=request.getParameter("qwechatstatus");
    	 if(qmobile!=null&&!qmobile.equals("")){
    		 map.put("qmobile",qmobile );
    		 request.setAttribute("qmobile", qmobile);
    	 }
    	 if(qusername!=null&&!qusername.equals("")){
    		 map.put("qusername", qusername);
    		 request.setAttribute("qusername", qusername);
    	 }
    	 if(qshopid!=null&&!qshopid.equals("")){
    		 map.put("qshopid",qshopid );
    		 request.setAttribute("qshopid",qshopid );
    	 }
    	 if(qenrollmode!=null&&!qenrollmode.equals("")){
    		 map.put("qenrollmode", qenrollmode);
    		 request.setAttribute("qenrollmode",qenrollmode );
    	 }
    	 if(qcustomuserid!=null&&!qcustomuserid.equals("")){
    		 map.put("qcustomuserid", qcustomuserid);
    		 request.setAttribute("qcustomuserid",qcustomuserid );
    	 }
    	 if(qnikename!=null&&!qnikename.equals("")){
    		 map.put("qnikename",qnikename );
    		 request.setAttribute("qnikename",qnikename );
    	 }
    	 if(qwechatstatus!=null&&!qwechatstatus.equals("")){
    		 map.put("qwechatstatus",qwechatstatus );
    		 request.setAttribute("qwechatstatus",qwechatstatus);
    	 }
    	 return map;
     }
     @RequestMapping("save")
     public String addBisCustomerUser(BisCustomUser user,@RequestParam("file")MultipartFile file,HttpServletRequest request){
    	 System.out.println(user.getUsername());
    	 @SuppressWarnings("deprecation")
		String path=request.getRealPath("/upload");
    	 File file1=new File(path);
    	 String parent=file1.getParent();
    	 SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
    	 parent=parent+"/upload/"+sdf.format(new Date());
    	 File dir=new File(parent);
    	 if(!dir.exists()){
    		 dir.mkdirs();
    	 }
    	 String filename=file.getOriginalFilename();
    	 File newfile=new File(dir,filename);
    	 if(!newfile.exists()){
    		 try {
				newfile.createNewFile();
				file.transferTo(newfile);
			} catch (IOException e) {
				e.printStackTrace();
			}
    	 }
    	 user.setPhoto("/upload/"+sdf.format(new Date())+"/"+file.getOriginalFilename());
    	 bisCustomUserService.addBisCustomUser(user);
    	 return "redirect:/custom/list";
     }
     @RequestMapping("delete")
     public String delBisCustomUser(String customUserId){
    	 this.bisCustomUserService.delBisCustomUser(customUserId);
    	 return "redirect:/custom/list";
     }
     @RequestMapping(value="toupdate",produces="application/json;charset=utf-8")
     @ResponseBody
     public String init(String customUserId){
    	 BisCustomUser custom=bisCustomUserService.getById(customUserId);
    	 String cus=JsonUtils.toJson(custom);
    	 return cus;
     }
     @RequestMapping("update")
     public String toupdate(BisCustomUser bisCustomUser,@RequestParam("file")MultipartFile file,HttpServletRequest request){
    	 @SuppressWarnings("deprecation")
		String path = request.getRealPath("/upload");//获取upload路径
 		 path = path+"/"+DateUtils.getYmdformat();//在upload下创建当前日期文件夹		
 		 String filename = file.getOriginalFilename();
 		if (StringUtils.isNotBlank(filename)) {
			File dir = new File(path);
			if (!dir.exists()) {
				dir.mkdirs();//如果不存在那么创建这个文件夹
			}
			File newfile = new File(dir,filename);
			if (!newfile.exists()) {
				try {
					newfile.createNewFile();
					file.transferTo(newfile);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			bisCustomUser.setPhoto(DateUtils.getYmdformat()+"/"+filename);
		}else {
			String id = bisCustomUser.getCustomUserId();
			BisCustomUser customUser = bisCustomUserService.getById(id);
			bisCustomUser.setPhoto(customUser.getPhoto());
		}
    	 bisCustomUserService.updBisCustomUser(bisCustomUser);
    	 return "redirect:/custom/list";
     }
     @RequestMapping("query")
     public String query(String customUserId){
    	 BisCustomUser custom=bisCustomUserService.getById(customUserId);
    	 String cuss=JsonUtils.toJson(custom);
    	 return cuss;
     }
     @RequestMapping("inite/{id}")
 	 public String inite(@PathVariable("id")String id,Model model){
 		 BisCustomUser customUser = bisCustomUserService.getById(id);
 		 model.addAttribute("cust", customUser);
 		 return "bcujsp/page1";
 	 }
}









